﻿#include "fileinfo.h"
#include "unitclass.h"
FileInfo::FileInfo(void)
{

}
bool FileInfo::updatePlate(const QString& card_id,bool is_In_Out)
{
    //扫描目录
    bool res = false;
    _carInfo.dt=QDateTime::currentDateTime();
    QString picDir = QString("%1/").arg("/home/bgj");
    if(!is_In_Out)
        picDir+="/On";
    else
        picDir+="/Out";
    //保存目录
    QString picSaveDir = QString("%1/").arg("/media/sda2/iBarn/public/uploads");
    QDir dir(picDir);
    QString IsOut;
    //目录存的则扫描目录
    if (dir.exists())
    {
        QStringList filters;
        filters.append("*.jpg");
        foreach (QFileInfo fileInfo, dir.entryInfoList(filters, QDir::Files))
        {
            QString baseName = fileInfo.completeBaseName();
            QStringList bnsl = baseName.split("_");
            qDebug()<<"bnsl"<<bnsl;
            QString chePflag,cId,Tu;
            if(bnsl.length()>=3)
            {
                IsOut=bnsl[0];
                _carInfo.PhotoTime = bnsl[2];
                _carInfo.plate =  bnsl[0];//取图片后的牌照
                _carInfo.plateColor = bnsl[1];
               // _carInfo.lane = bnsl[3];
                if(_carInfo.PhotoTime.contains("车牌"))
                {
                    chePflag="cpt";
                }else{
                    chePflag="qjt";
                }
            }
            //是否有车牌,有则返回
            if(_carInfo.PhotoTime.length()>0)
            {
                //save picture
                QDir saveDir(picSaveDir);
                if(!saveDir.exists())
                {
                    saveDir.mkdir(picSaveDir);
                }
                picSaveDir += baseName;
                picSaveDir += ".jpg";
                usleep(300000);
                int  fileSize=fileInfo.size();
                if(fileSize > 1024*70)
                {
                    QFile::copy("camera.jpg",picSaveDir);
                    QFile::remove("camera.jpg");
                }else{
                    QFile::copy(fileInfo.absoluteFilePath(),picSaveDir);
                }
                //不做压缩处理
                QFile::copy(fileInfo.absoluteFilePath(),picSaveDir);


                if(chePflag=="cpt")
                {
                    _carInfo.p1 = picSaveDir;
                    _carInfo.p2="";
                    Tu= picSaveDir;
                }else{
                    _carInfo.p1="";
                    _carInfo.p2 = picSaveDir;
                    Tu= picSaveDir;
                }
                QFile  defilr(fileInfo.filePath());
                defilr.remove();
                Tu=Tu.mid(24);
                if(IsOut!="out" && !is_In_Out)
                {
                    if(datebase::GetInstance()->UpdateImage(&(datebase::GetInstance()->mySql_db),card_id,Tu,false))
                    {
                        Tu.clear();
                        return true;
                    }
                }
                if(IsOut=="out" && is_In_Out){
                    if(datebase::GetInstance()->UpdateImage(&(datebase::GetInstance()->mySql_db),card_id,Tu,true))
                    {
                        Tu.clear();
                        return true;
                    }
                }
                Tu.clear();
            }
        }
    }
    //QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    return res;
}
//压缩图片在70K以内
void FileInfo::fileScaled(QString fileName,QString num)
{
    float count;
    count=num.toFloat();
    if(fileName.isEmpty())
    {
        qDebug()<<"打开图片失败!";
        return;
    }
    QImageReader imgReader;
    imgReader.setFileName(fileName);
    if (!imgReader.canRead())
    {
        qDebug()<<"The image data is corrupt, Please select a valid one!";
        return;
    }
    QImage img(fileName);
    int wideg=img.width();
    int hei=img.height();
    img=img.scaled(wideg*count,hei*count,Qt::KeepAspectRatio);//Qt::KeepAspectRatio自适应大小，不变形
    if(img.save("camera.jpg"))
        qDebug()<<"compress pic  success!";
    else
        qDebug()<<"compress pic  failed";
}

bool FileInfo::deleteFullDisk()
{
    QString picDir = QString("%1/%2").arg(systemFile::GetInstance()->ftpDir).arg("picture");
    QDir dir(picDir);
    //目录存的则扫描目录
    if (dir.exists())
    {
        QStringList filters;
        QString data="";
        QString deldir="";
        foreach (QFileInfo fileInfo, dir.entryInfoList(filters, QDir::Dirs))
        {
            QString baseName = fileInfo.completeBaseName();
            QString absName=fileInfo.absoluteFilePath();
            if(baseName!="" && baseName!=".")
            {
                if(data<baseName && data == "")
                {
                    data=baseName;
                    deldir=absName;
                }
                if(data>baseName)
                {
                    data=baseName;
                    deldir=absName;
                }
            }

        }
        deldir="rm -rf "+deldir;
        qDebug()<<"deldir="<<deldir;
        if(::system(deldir.toLatin1().data())==0)
            return true;
        else
            return false;
    }
    return false;
}

